"""
Case Type   : 共享存储-系统内部工具-dsscmd
Case Name   : dsscmd工具新建目录之-d dir_name参数验证
Create At   : 2023/4/13
Owner       : opentestcase019
Description :
    1.新建目录，dir_name正常命名
    2.新建目录，dir_name包含特殊字符
    3.新建目录，dir_name为已存在的目录名
    4.新建目录，dir_name目录命名长度超过64
    5.新建目录，dir_name目录命名长度等于64
    6.新建目录，dir_name目录命名长度等于63
    7.新建目录，dir_name目录命名包含中文
    8.新建目录，dir_name目录命名为纯数字
    9.新建目录，dir_name目录命名为纯字母
Expect      :
    1.创建成功
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
    6.创建成功
    7.合理报错
    8.创建成功
    9.创建成功
History     :
"""
import os
import unittest
from yat.test import Node
from yat.test import macro
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common


class ShareStorageTool(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.primary_node = Node('PrimaryDbUser')
        self.common = Common()
        self.dir_name = "dir_dsscmd_0019"
        self.dir_name_65chars = \
            "dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019_d"
        self.dir_name_64chars = \
            "dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019_"
        self.dir_name_63chars = \
            "dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019_dir_dsscmd_0019"
        self.dir_name_numbers = "0019"
        self.dir_name_letters = "dirdsscmd"
        self.logger.info("-----获取卷组名-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};dsscmd lsvg"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn("Succeed to display lsvg info.", execute_msg, "获取卷组名失败")
        self.vgname = execute_msg.splitlines()[1].split()[0].strip()
        self.logger.info(self.vgname)
        self.path_1 = os.path.join(self.vgname, self.dir_name)
        self.path_2 = os.path.join(self.vgname, self.dir_name_63chars)
        self.path_3 = os.path.join(self.vgname, self.dir_name_numbers)
        self.path_4 = os.path.join(self.vgname, self.dir_name_letters)

    def test_sharestorage_tool(self):
        step1 = "-----step1:新建目录，dir_name正常命名;expect:创建成功-----"
        self.logger.info(step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name}.",
                      execute_msg, "执行失败" + step1)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd ls -p +{self.vgname}"
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(self.dir_name, execute_msg, "执行失败" + step1)

        step2 = "-----step2:新建目录，dir_name包含特殊字符;expect:合理报错-----"
        self.logger.info(step2)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name}$"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path {self.dir_name}$ "
                      f"decode error , name should be [0~9,a~z,A~Z,-,_,.].",
                      execute_msg, "执行失败" + step2)

        step3 = "-----step3:新建目录，dir_name为已存在的目录名;expect:合理报错-----"
        self.logger.info(step3)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Make dir or Create file failed, {self.dir_name} "
                      f"has already existed", execute_msg, "执行失败" + step3)

        step4 = "-----step4:新建目录，dir_name目录命名长度超过64;expect:合理报错-----"
        self.logger.info(step4)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_65chars}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path {self.dir_name_65chars} "
                      f"decode error , name is too long.",
                      execute_msg, "执行失败" + step4)

        step5 = "-----step5:新建目录，dir_name目录命名长度等于64;expect:合理报错-----"
        self.logger.info(step5)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_64chars}"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path {self.dir_name_64chars} "
                      f"decode error , name is too long.",
                      execute_msg, "执行失败" + step5)

        step6 = "-----step6:新建目录，dir_name目录命名长度等于63;expect:合理报错-----"
        self.logger.info(step6)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_63chars};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_63chars}.",
                      execute_msg, "执行失败" + step6)

        step7 = "-----step7:新建目录，dir_name目录命名包含中文;expect:合理报错-----"
        self.logger.info(step7)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name}目录;"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"cmd mkdir error:2193 Path {self.dir_name}目录 "
                      f"decode error , name should be [0~9,a~z,A~Z,-,_,.].",
                      execute_msg, "执行失败" + step6)

        step8 = "-----step8:新建目录，dir_name正常命名;expect:创建成功-----"
        self.logger.info(step8)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_numbers};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_numbers}.",
                      execute_msg, "执行失败" + step8)

        step9 = "-----step9:新建目录，dir_name正常命名;expect:创建成功-----"
        self.logger.info(step9)
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd mkdir -p +{self.vgname} -d {self.dir_name_letters};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to make dir, path is +{self.vgname}, "
                      f"dir name is {self.dir_name_letters}.",
                      execute_msg, "执行失败" + step9)

    def tearDown(self):
        self.logger.info("-----恢复环境-----")
        execute_cmd = f"source {macro.DB_ENV_PATH};" \
            f"dsscmd rmdir -p +{self.path_1};" \
            f"dsscmd rmdir -p +{self.path_2};" \
            f"dsscmd rmdir -p +{self.path_3};" \
            f"dsscmd rmdir -p +{self.path_4};"
        self.logger.info(execute_cmd)
        execute_msg = self.common.get_sh_result(self.primary_node, execute_cmd)
        self.assertIn(f"Succeed to rm dir, path is +{self.path_1}",
                      execute_msg, "恢复环境失败")
        self.assertIn(f"Succeed to rm dir, path is +{self.path_2}",
                      execute_msg, "恢复环境失败")
        self.assertIn(f"Succeed to rm dir, path is +{self.path_3}",
                      execute_msg, "恢复环境失败")
        self.assertIn(f"Succeed to rm dir, path is +{self.path_4}",
                      execute_msg, "恢复环境失败")
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
